package com.example.personalcenter.onlyPerson;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.personalcenter.MainActivity;
import com.example.personalcenter.R;
import com.example.personalcenter.Tool.OKHttpTool;
import com.example.personalcenter.onlyPerson.bean.Content;
import com.example.personalcenter.onlyPerson.bean.User;
import com.google.gson.Gson;

import org.json.JSONObject;

public class LoginActivity extends AppCompatActivity {
    private TextView message;
    private EditText editTextUsername;
    private EditText editTextPassword;
    private Button butLogin;
    private Gson gson;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }


    private void getLoginData(String username, String password) {
        User user = new User();
        user.setPassword(password);
        user.setUsername(username);

        String  jsonObject =gson.toJson(user);

        OKHttpTool.deNetRequest("prod-api/api/login",
                OKHttpTool.POST,
                Content.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        Content content = (Content) obj;
                        if (content.getCode() == 200){
                            OKHttpTool.TOKEN = content.getToken();
                            save(content.getToken());
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(LoginActivity.this, content.getMsg(), Toast.LENGTH_LONG).show();
                                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                    intent.putExtra("isToMeFragment", "y");
                                    startActivity(intent);
                                    LoginActivity.this.finish();
                                }
                            });
                        }else if (content.getCode() == 500){
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(LoginActivity.this, content.getMsg(), Toast.LENGTH_LONG).show();
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(String msg) { }
                },jsonObject);

    }

    private void save(String token) {
        SharedPreferences sharedPreferences = getSharedPreferences("user_data", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("auth_token",token);
        editor.apply();
        OKHttpTool.TOKEN = token;
    }

    private void initView() {
        message = findViewById(R.id.message);
        editTextUsername = findViewById(R.id.editTextUsername);
        editTextPassword = findViewById(R.id.editTextPassword);
        butLogin = findViewById(R.id.but_login);

        butLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = editTextUsername.getText().toString();
                String password = editTextPassword.getText().toString();

                if (!username.isEmpty() && !password.isEmpty()){
                    getLoginData(username,password);
                }else {
                    Toast.makeText(LoginActivity.this,"用户名或密码不能为空",Toast.LENGTH_SHORT).show();
                }
            }
        });

        gson = new Gson();
        handler = new Handler();
    }
}